package com.mkyong.rest;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.mkyong.Track;
import com.projectY.dao.DataDb;

@Path("/extension")
public class JSONService {

	@GET
	@Path("/get")
	@Produces(MediaType.APPLICATION_JSON)
	public Track getTrackInJSON() {

		Track track = new Track();
		track.setTitle("Enter Sandman");
		track.setSinger("Metallica");

		return track;

	}

	@POST
	@Path("/getLinkUpvotes")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createTrackInJSON(String track) throws JSONException, SQLException {
//		{"links":["http://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html",
//		          "http://dl.acm.org/citation.cfm?id=73040","http://en.wikipedia.org/wiki/Heap_(data_structure)",
//		          "http://en.wikipedia.org/wiki/Heap_(data_structure)"],"friends":["arushi","sumit","vrinda","ishu"],"site":"twitter"}
		System.out.println(track+" track");
		JSONObject obj=new JSONObject(track);
		JSONArray linksArray= obj.getJSONArray("links");
		ArrayList<String> links=new ArrayList<String>();
		for(int i=0;i<linksArray.length();i++)
		{
			links.add(linksArray.getString(i));
		}
		String site=obj.getString("site");
		JSONArray friendsArray= obj.getJSONArray("friends");
		ArrayList<String> friends=new ArrayList<String>();
		for(int i=0;i<friendsArray.length();i++)
		{
			friends.add(friendsArray.getString(i).toLowerCase());
		}
		System.out.println(links+" \n "+friends);
		JSONObject res=new JSONObject(new DataDb().readLinksProfile(links,friends));
		res.put("site", site);
		return Response.status(201).entity(res).build();
	}
	@POST
	@Path("/upVote")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response upVote(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","user":"ishu"}
		JSONObject obj=new JSONObject(track);
		System.out.println(track);
		int res= new DataDb().Vote(obj.getString("link"),obj.getString("user"),obj.getInt("vote"),obj.getString("query"),obj.getInt("tty"));
		System.out.println(res);
		JSONObject result = new JSONObject();


		if(res==-1)
		{
			result.put("result","alreadyThere");
		}
		else
		if(res==1)
		{
			result.put("result","success");
		}
		else
		{
			result.put("result","failure");
		}
		return Response.status(201).entity(result).build();
	}
	
	@POST
	@Path("/getWow")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response getWow(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","user":"ishu","site":"wow"}
		System.out.println(track);
		JSONObject obj=new JSONObject(track);
		JSONArray linksArray= obj.getJSONArray("links");
		HashMap<String,Integer> res=new HashMap<String,Integer>();
		for(int i=0;i<linksArray.length();i++)
		{
			res.put((String)linksArray.get(i),(int)(System.currentTimeMillis()%100)+i);
		}
		JSONObject result = new JSONObject(res);
		Iterator it = res.entrySet().iterator();
		System.out.println("##############################################################################################################");
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
	        it.remove(); // avoids a ConcurrentModificationException
	    }
	    System.out.println("--------------------------------------------------------------------------------------------------------------");
	    String site=obj.getString("site");
		result.put("site", site);
		return Response.status(201).entity(result).build();
	}
	@POST
	@Path("/storeTimeTaken")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response storeTimeTaken(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","user":"ishu"}
		track.replace("\\", " ");
		JSONObject obj=new JSONObject(track);
		System.out.println(track);
		int res= new DataDb().storeTimTaken(obj.getString("link"),obj.getString("twitter"),obj.getString("facebook"),obj.getString("gapi") ,obj.getInt("tty"));
		System.out.println(res);
		JSONObject result = new JSONObject();


		if(res==-1)
		{
			result.put("result","alreadyThere");
		}
		else
		if(res==1)
		{
			result.put("result","success");
		}
		else
		{
			result.put("result","failure");
		}
		return Response.status(201).entity(result).build();
	}
	@POST
	@Path("/downVote")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response downVote(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","user":"ishu"}
		JSONObject obj=new JSONObject(track);
		int res= new DataDb().downVote(obj.getString("link"),obj.getString("user"),obj.getInt("vote"),obj.getString("query"));
		System.out.println(res);
		JSONObject result = new JSONObject();


		if(res==-1)
		{
			result.put("result","alreadyThere");
		}
		else
		if(res==1)
		{
			result.put("result","success");
		}
		else
		{
			result.put("result","failure");
		}
		return Response.status(201).entity(result).build();
	}
	@POST
	@Path("/upVotingFriends")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response upVotingFriends(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","friends":["ishu","arushi"]}
		JSONObject obj=new JSONObject(track);
		JSONArray friendsArray= obj.getJSONArray("friends");
		ArrayList<String> friends=new ArrayList<String>();
		for(int i=0;i<friendsArray.length();i++)
		{
			friends.add(friendsArray.getString(i));
		}
		JSONArray res=new JSONArray(new DataDb().upVotingFriends(obj.getString("link"),friends));
		System.out.println(res);
		return Response.status(201).entity(res).build();
	}
	
	@POST
	@Path("/isVoted")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response isVoted(String track) throws JSONException, SQLException {
//		{"link":"http://www.mkyong.com/jdbc/jdbc-sasa-example-insert-a-record","friends":["ishu","arushi"]}
		JSONObject obj=new JSONObject(track);
		JSONObject res=new JSONObject( );
		System.out.println(obj.getString("user") +" "+obj.getString("link") );
		res.put("result", new DataDb().checkIfUpVoted(obj.getString("link"),obj.getString("user"))?1:-1 );
		return Response.status(201).entity(res).build();
	}
}